<?php 
/**
 * 赠品活动
 * $Author: liubo $
 * $Id: bonus.php 17217 2011-01-19 06:29:08Z liubo $
*/


// exit();
define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

// /* act操作项的初始化 */
if (empty($_REQUEST['act']))
{
    $_REQUEST['act'] = 'list';
}
else
{
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

// 礼包列表
if($_REQUEST['act'] == "list" || $_REQUEST['act'] == 'query'){
    admin_priv('gift_manage');
    /* 删除无效活动 */
    $sql = "DELETE FROM " . $ecs->table('activity') . " WHERE name = ''";
    $db->query($sql);
    $filter['page']                      = empty($_REQUEST['page']) ? 1 : $_REQUEST['page'];
    $filter['page_size']                 = empty($_REQUEST['page_size']) ? 15 : $_REQUEST['page_size'];
    $smarty->assign('ur_here', "赠品列表");
    $smarty->assign('action_link', array('href' => 'activity_gift.php?act=add', 'text' => "添加赠品"));
    if(empty($_REQUEST['is_ajax'])){
        $smarty->assign('full_page',    1);
    }
    //获取礼包列表
    $sql = "SELECT * FROM " . $ecs->table('activity') .
            " WHERE extension_code = 'gift' ORDER BY parent_id DESC LIMIT " . ($filter['page']-1)*$filter['page_size'] . ", $filter[page_size]";
    $list = $db->getAll($sql);
    foreach ($list as $key => $value) {
        $sql = "SELECT valid_number,cart_number,total_number FROM".$ecs->table('activity_stock') ." WHERE parent_id = ".$value['parent_id'];

        $list[$key]['number'] = $db->getRow($sql);
        $list[$key]['add_time'] = date("Y-m-d H:i:s", $value['add_time']);
        $list[$key]['update_time'] = date("Y-m-d H:i:s", $value['update_time']);
    }
    $smarty->assign('list',   $list);
    $smarty->assign('filter',       $filter);
    $smarty->assign('record_count', count($list));
    $smarty->assign('page_count',   intval(count($list)/15)+1);
    if(empty($_REQUEST['is_ajax'])){
        $smarty->display('activity_gift_list.htm');
    }else{
        make_json_result($smarty->fetch('activity_gift_list.htm'),'', array('filter' => $filter, 'page_count' => intval(count($list)/15)+1));
    }
}
/* 添加、修改 */
elseif($_REQUEST['act'] == "add" || $_REQUEST['act'] == 'edit'){
    admin_priv('gift_manage');
    !empty($_REQUEST['parent_id']) && $parent_id = $_REQUEST['parent_id'];
    if($_REQUEST['act'] == 'edit'){
        /* 获取礼包信息 */
        $sql = "SELECT * FROM " . $ecs->table('activity') . " WHERE parent_id = '$parent_id'";
        $info = $db->getRow($sql);
        $info['start_time'] =!empty($info['start_time'])?date('Y-m-d',$info['start_time']).'T'.date('H:i',$info['start_time']):'' ;
        $info['end_time']   =!empty($info['end_time'])?date('Y-m-d',$info['end_time']).'T'.date('H:i',$info['end_time']):'' ;
        $form_act = 'update';
    }else{
        $info['extension_code'] = 'gift';
        $parent_id = $info['parent_id'] = '0';
        $form_act = 'insert';
    }

    /* 获取商品信息 */
    $sql = "SELECT ag.id, g.goods_name, ga.attr_value, ag.goods_number FROM " . $ecs->table('activity_goods') . " ag LEFT JOIN " . $ecs->table('goods') . " g ON g.goods_id = ag.goods_id LEFT JOIN " . $ecs->table('goods_attr') . " ga ON ga.goods_attr_id = ag.goods_attr_id WHERE ag.parent_id = '$parent_id'";
    $goods_list = $db->getAll($sql);
    foreach ($goods_list AS $key => $val)
    {
        $opt[] = array('value' => $val['id'],
                        'text' => ($val['attr_value']?($val['goods_name'].'【'.$val['attr_value'].'】'):$val['goods_name'])."*$val[goods_number]");
    }

    $sql = "SELECT *, 0 AS valid_number, 0 AS cart_number, 0 AS total_number FROM ".$ecs->table('service_area');
    $stockList = $db->getAll($sql);
    /* 获取所有发放信息 */
    if($parent_id){
        foreach ($stockList as $key => $value) {
            $sql = "SELECT * FROM " . $ecs->table('activity_stock') . " acts LEFT JOIN ".$ecs->table('service_area')." sa ON sa.area_id = acts.area_id WHERE acts.parent_id = $parent_id AND acts.area_id = $value[area_id]";
            if($row = $db->getRow($sql)){
                $stockList[$key] = $row;
            }
        }
    }
    $smarty->assign('stockList',  $stockList);
    $smarty->assign('form_act', $form_act);
    $smarty->assign('act', $_REQUEST['act']);
    $smarty->assign('info', $info);
    $smarty->assign('goods_list', $opt);
    $smarty->display('activity_gift_info.htm');
}
/* 处理添加、修改 */
elseif($_REQUEST['act'] == "insert" || $_REQUEST['act'] == 'update'){
    admin_priv('gift_manage');
    include_once(ROOT_PATH . '/includes/cls_image.php');
    $image = new cls_image($_CFG['bgcolor']);
    $post['name']           = !empty($_POST['name'])?trim($_POST['name']):'';
    $post['price']          = !empty($_POST['price'])?floatval($_POST['price']):'0.00';
    $post['min_price']      = !empty($_POST['min_price'])?floatval($_POST['min_price']):'0';
    $post['max_price']      = !empty($_POST['max_price'])?floatval($_POST['max_price']):'99999';
    $post['original_price'] = !empty($_POST['original_price'])?floatval($_POST['original_price']):'0';
    $post['is_delete']      = !empty($_POST['is_delete'])?intval($_POST['is_delete']):'0';
    $post['is_sale']        = !empty($_POST['is_sale'])&&empty($_POST['is_delete'])?intval($_POST['is_sale']):'0';
    $post['parent_id']      = !empty($_POST['parent_id'])?intval($_POST['parent_id']):'0';
    $post['content']        = !empty($_POST['content'])?trim($_POST['content']):'';
    $post['start_time']     = !empty($_POST['start_time'])?strtotime($_POST['start_time']):'0';
    $post['end_time']       = !empty($_POST['end_time'])?strtotime($_POST['end_time']):'0';
    $post['update_time']    = time();
    $post['add_time']       = time();
    /* 插入还是更新的标识 */
    $is_insert = $_REQUEST['act'] == 'insert';

    if(!$is_insert){
        /* 查询原有礼包信息 */
        $sql = "SELECT * " .
                " FROM " . $ecs->table('activity') .
                " WHERE parent_id = '$post[parent_id]'";
        $row = $db->getRow($sql);
        if(!$row){
            sys_msg('未找到相关礼包，请重新输入', 1, array(), false);
        }
    }


    /* 检查数据完整性 */
    if(!$post['name'] || (empty($post['price']) && $post['price'] != '0.00')){
        sys_msg('请输入完整信息', 1, array(), false);
    }else{
        $sql = "SELECT * FROM " . $ecs->table('activity_goods') . " WHERE parent_id = $post[parent_id]";
        if(!$db->getAll($sql)){
            sys_msg('请增加商品信息', 1, array(), false);
        }
    }

    /* 检查图片：如果有错误，检查尺寸是否超过最大值；否则，检查文件类型 */
    if (isset($_FILES['img']['error'])) // php 4.2 版本才支持 error
    {
        // 最大上传文件大小
        $php_maxsize = ini_get('upload_max_filesize');
        $htm_maxsize = '2M';
        // 商品图片
        if ($_FILES['img']['error'] == 0)
        {
            if (!$image->check_img_type($_FILES['img']['type']))
            {
                sys_msg('图片格式不正确！', 1, array(), false);
            }
        }
        elseif ($_FILES['img']['error'] == 1)
        {
            sys_msg(sprintf('图片文件太大了（最大值：%s），无法上传。', $php_maxsize), 1, array(), false);
        }
        elseif ($_FILES['img']['error'] == 2)
        {
            sys_msg(sprintf('图片文件太大了（最大值：%s），无法上传。', $htm_maxsize), 1, array(), false);
        }
    }


    if($_FILES['img']['tmp_name'] != '' && $_FILES['img']['tmp_name'] != 'none'){
        if(!$is_insert){
            /* 删除原来的图片文件 */
            if ($row['img'] != '' && is_file('../' . $row['img']))
            {
                @unlink('../' . $row['img']);
            }
        }
        $post['img']   = $image->upload_image($_FILES['img']);
    }else{
        $post['img']   = $row['img'];
    }



    /*活动信息*/
        $activity_info = array(
        'extension_code' => 'gift',
        'name'           => $post['name'],
        'price'          => $post['price'],
        'min_price'      => $post['min_price'],
        'max_price'      => $post['max_price'],
        'original_price' => $post['original_price'],
        'img'            => $post['img'],
        'content'        => $post['content'],
        'is_delete'      => $post['is_delete'],
        'is_sale'        => $post['is_sale'],
        'add_time'       => $post['add_time'],
        'update_time'    => $post['update_time'],
        'start_time'     => $post['start_time'],
        'end_time'       => $post['end_time'],
        );



    if($is_insert){
        $db->autoExecute($ecs->table('activity'), $activity_info, 'INSERT', '', 'SILENT');
        if($parent_id = $db->insert_id()){
            /* 插入至发放库存表 */
            $sql = "UPDATE " . $ecs->table('activity_goods') . " SET parent_id = $parent_id WHERE parent_id = 0";
            $db->query($sql);
            admin_log($post['name']."parent_id:$parent_id", 'add', 'gift');
        }else{
            sys_msg('已成功添加失败，请联系管理员', 0, $link);
        }
    }else{
        if($row['is_delete'] != 1 && $post['is_delete'] == 1){
            /* 如果回收 */
            // 加锁
            require_once(ROOT_PATH . 'includes/classLock.php');
            $lock = new classLock;
            $lockName = 'lockGift' . $post['parent_id'];
            if ($lock->lock($lockName)) {
                delete_activity($post['parent_id']);
                admin_log($post['name']."parent_id:$post[parent_id]", 'remove', 'gift');
                // 释放锁
                $lock->unlock($lockName);
            }
        }
        /*记录管理员操作*/
        admin_log($post['name']."parent_id:$post[parent_id]", 'edit', 'gift');
        $db->autoExecute($ecs->table('activity'), $activity_info, 'UPDATE', 'parent_id = '.$post['parent_id'], 'SILENT');

    }
    /* 提示页面 */
    $link[0] = array('href' => 'activity_gift.php?act=list', 'text' => '返回赠品列表');
    sys_msg($is_insert ? '已成功添加赠品' : '已成功修改赠品', 0, $link);
    $smarty->display('activity_gift_info.htm');
}
/*------------------------------------------------------ */
//-- 搜索商品
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'get_goods_list')
{
    include_once(ROOT_PATH . 'includes/cls_json.php');
    $json = new JSON;

    $where = ' 1 ';
    $keyword = $_REQUEST['keyword'];
    if(!empty($keyword)){
        $where .= " AND (g.goods_sku = '$keyword' OR ga.attr_sku = '$keyword' OR g.goods_name LIKE '%$keyword%')";
    }
    /* 取得数据 */
    $sql = 'SELECT * FROM ' . $GLOBALS['ecs']->table('goods') . ' g LEFT JOIN '  . $GLOBALS['ecs']->table('goods_attr') . ' ga ON ga.goods_id = g.goods_id WHERE '. $where .
           'LIMIT 150';
    $row = $GLOBALS['db']->getAll($sql);

    foreach ($row AS $key => $val)
    {
        $opt[] = array('value' => ($val['attr_sku']?$val['attr_sku']:$val['goods_sku']),
                        'text' => ($val['attr_value']?$val['goods_name'].'【'.$val['attr_value'].'】':$val['goods_name']),
                        'data' => $val['shop_price']);
    }
    if(!empty($opt)){
        $result['error'] = 0;
        $result['content'] = $opt;
    }else{
        $result['error'] = '1';
        $result['message'] = '未搜索到有关商品';
    }
    echo $json->encode($result,false);
    exit;
}
/* 添加商品 */
elseif($_REQUEST['act'] == "add_goods"){
    admin_priv('gift_manage');
    include_once(ROOT_PATH . 'includes/cls_json.php');
    $json = new JSON;

    $sku=intval($_REQUEST['sku']);
    $num=intval($_REQUEST['num']);
    $parent_id=intval($_REQUEST['parent_id']);
    /* 获取商品id和属性id */
    $sql = "SELECT goods_id,goods_attr_id FROM " . $ecs->table('goods_stock') . " WHERE goods_sku = '$sku'";
    $goods = $db->getRow($sql);
    if($goods){
        $sql = "SELECT * FROM " . $ecs->table('activity_goods') . " WHERE goods_id = '$goods[goods_id]' AND goods_attr_id = '$goods[goods_attr_id]' AND parent_id = '$parent_id' ";
        if(!$db->getOne($sql)){
            $sql = "INSERT INTO " . $ecs->table('activity_goods') . "(parent_id, goods_id, goods_attr_id, goods_number) VALUES('$parent_id', '$goods[goods_id]', '$goods[goods_attr_id]', '$num')";
            $db->query($sql);
            $id = $db->insert_id();
            if($id){
                $sql = "SELECT ag.id, g.goods_name, ga.attr_value, ag.goods_number FROM " . $ecs->table('activity_goods') . " ag LEFT JOIN " . $ecs->table('goods') . " g ON g.goods_id = ag.goods_id LEFT JOIN " . $ecs->table('goods_attr') . " ga ON ga.goods_attr_id = ag.goods_attr_id WHERE ag.parent_id = '$parent_id'";
                $goods_list = $db->getAll($sql);
                foreach ($goods_list AS $key => $val)
                {
                    $opt[] = array('value' => $val['id'],
                                    'text' => ($val['attr_value']?($val['goods_name'].'【'.$val['attr_value'].'】'):$val['goods_name'])."*$val[goods_number]");
                }
                $result['error'] = 0;
                $result['content'] = $opt;
            }else{
                $result['error'] = 1;
                $result['message'] = "添加出错，请联系管理员";
            }
        }else{
            $result['error'] = 1;
            $result['message'] = "商品已存在，请勿重复添加";
        }
    }
    else{
        $result['error'] = 1;
        $result['message'] = "商品信息错误，请联系管理员";
    }
    echo $json->encode($result,false);
    exit();
}
/* 删除商品 */
elseif($_REQUEST['act'] == "drop_goods"){
    admin_priv('gift_manage');
    include_once(ROOT_PATH . 'includes/cls_json.php');
    $json = new JSON;
    $id=intval($_REQUEST['id']);
    $parent_id=intval($_REQUEST['parent_id']);
    $sql = "SELECT * FROM " . $ecs->table('activity_goods') . " WHERE id = '$id' AND parent_id = '$parent_id'";
    if($db->getRow($sql)){
        $sql = "DELETE FROM " . $ecs->table('activity_goods') . " WHERE id = '$id'";
        $db->query($sql);
        $sql = "SELECT ag.id, g.goods_name, ga.attr_value, ag.goods_number FROM " . $ecs->table('activity_goods') . " ag LEFT JOIN " . $ecs->table('goods') . " g ON g.goods_id = ag.goods_id LEFT JOIN " . $ecs->table('goods_attr') . " ga ON ga.goods_attr_id = ag.goods_attr_id WHERE ag.parent_id = '$parent_id'";
        $goods_list = $db->getAll($sql);
        foreach ($goods_list AS $key => $val)
        {
            $opt[] = array('value' => $val['id'],
                            'text' => ($val['attr_value']?$val['goods_name'].'【'.$val['attr_value'].'】':$val['goods_name'])."*$val[goods_number]");
        }
        $result['error'] = 0;
        $result['content'] = $opt;
    }else{
        $result['error'] = 1;
        $result['message'] = "商品信息错误，请联系管理员";
    }
    echo $json->encode($result,false);
    exit();
}
/* 查询发放信息 */
elseif($_REQUEST['act'] == "getStock"){
    admin_priv('gift_manage');
    include_once(ROOT_PATH . 'includes/cls_json.php');
    $json = new JSON;
    $parent_id=intval($_REQUEST['parent_id']);
    /* 获取所有发放信息 */
    $sql = "SELECT *, 0 AS valid_number, 0 AS cart_number, 0 AS total_number FROM ".$ecs->table('service_area');
    $arr = $db->getAll($sql);
    if($parent_id){
        foreach ($arr as $key => $value) {
            $sql = "SELECT * FROM " . $ecs->table('activity_stock') . " acts LEFT JOIN ".$ecs->table('service_area')." sa ON sa.area_id = acts.area_id WHERE acts.parent_id = $parent_id AND acts.area_id = $value[area_id]";
            $row = $db->getRow($sql);
            if($row){
                $arr[$key] = $row;
            }
        }
    }
    foreach ($arr as $key => $value) {
        $stockList[$value['area_id']]['area_name'] = $value['area_name'];
        $stockList[$value['area_id']]['valid_number'] = $value['valid_number'];
        $stockList[$value['area_id']]['cart_number'] = $value['cart_number'];
        $stockList[$value['area_id']]['total_number'] = $value['total_number'];
        $stockList[$value['area_id']]['stock'] = 999999;
    }
    /* 获取所有商品信息 */
    $sql = "SELECT ag.id, ag.goods_attr_id, gs.goods_sku, ag.goods_id, ag.goods_number FROM " . $ecs->table('activity_goods') . " ag LEFT JOIN " . $ecs->table('goods_stock') . " gs ON gs.goods_id = ag.goods_id AND gs.goods_attr_id = ag.goods_attr_id AND area_id = 3 WHERE ag.parent_id = '$parent_id'";
    $goods_list = $db->getAll($sql);
    /* 初始化库存类 */
    require_once(ROOT_PATH . 'includes/classStock.php');
    $cStock = new classStock();
    /* 获取还可发放信息 */
    foreach ($goods_list as $key => $value) {
        $value['stock'] = $cStock->selectStock($value['goods_sku']);
        $stock = intval($value['stock']/$value['goods_number']);
        $stockList[3]['stock'] = !isset($stockList[3]['stock']) ? $stock : MIN($stock,$stockList[3]['stock']);
    }
    $result['error'] = '0';
    $result['content'] = $stockList;
    echo $json->encode($result,false);
    exit();
}
/* 设置发放信息 */
elseif($_REQUEST['act'] == "setStock"){
    admin_priv('gift_manage');
    include_once(ROOT_PATH . 'includes/cls_json.php');
    $json = new JSON;
    $parent_id=intval($_REQUEST['parent_id']);
    $num=intval($_REQUEST['num']);
    $area_id=intval($_REQUEST['area_id']);
    if(empty($parent_id) || empty($num) || empty($area_id)){
        $result['error'] = '1';
        $result['message'] = '参数错误，请联系管理员';
        echo $json->encode($result,false);
        exit();
    }
    /* 获取礼包状态 */
    $sql = "SELECT * FROM " . $ecs->table('activity') . " WHERE parent_id = '$parent_id'";
    $info = $db->getRow($sql);
    if($info['is_delete'] != 0){
        $result['error'] = '1';
        $result['message'] = '礼包当前状态不允许发放';
        echo $json->encode($result,false);
        exit();
    }
    // 加锁
    require_once(ROOT_PATH . 'includes/classLock.php');
    $lock = new classLock;
    $lockName = 'lockGift' . $parent_id;
    if ($lock->lock($lockName)) {    /* 初始化库存类 */
        require_once(ROOT_PATH . 'includes/classStock.php');
        $cStock = new classStock();

        /* 获取所有商品信息 */
        $sql = "SELECT ag.id, ag.goods_attr_id, ag.goods_id, gs.goods_sku, ag.goods_number FROM " . $ecs->table('activity_goods') . " ag ".
            "LEFT JOIN " . $ecs->table('goods_stock') . " gs ON gs.goods_id = ag.goods_id AND gs.goods_attr_id = ag.goods_attr_id AND gs.area_id = 3 WHERE ag.parent_id = '$parent_id'";
        $goods_list = $db->getAll($sql);
        /* 获取还可发放数量 */
        foreach ($goods_list as $key => $value) {
            $value['stock'] = $cStock->selectStock($value['goods_sku']);
            $stock = intval($value['stock']/$value['goods_number']);
            $stocks = !isset($stocks) ? $stock : MIN($stock,$stocks);
        }
        if($stocks < $num){
            $result['error'] = '1';
            $result['message'] = '发放数量超出最大可发放数量，不允许发放';
            echo $json->encode($result,false);
            exit();
        }else{
            /* 预减库存 */
            foreach ($goods_list as $key => $value) {
                $minusStock = -$value['goods_number'] * $num;
                $cStock->changeStock($value['goods_sku'], $minusStock);
            }
            $sql = "SELECT stockid FROM " . $ecs->table('activity_stock') . " WHERE area_id = '$area_id' AND parent_id = '$parent_id' ";
            if($stockid = $db->getOne($sql)){
                /* 添加发放数量 */
                $sql = "UPDATE " . $ecs->table('activity_stock') . " SET valid_number = valid_number + $num, total_number = total_number + $num WHERE stockid = $stockid";
                $db->query($sql);
            }else{
                /* 新添加发放数量 */
                $sql = "INSERT INTO " . $ecs->table('activity_stock') . " (parent_id, area_id, valid_number, cart_number, total_number) VALUES('$parent_id', '$area_id', '$num', '0', '$num')";
                $db->query($sql);
            }
        }
        $result['error'] = '0';
        $result['content'] = '发放成功';
        // 释放锁
        $lock->unlock($lockName);
    }
    echo $json->encode($result,false);
    exit();
}
/* 设置上架状态 */
elseif($_REQUEST['act'] == 'toggle_on_sale'){
    admin_priv('gift_manage');
    $parent_id       = intval($_POST['id']);
    $is_sale        = intval($_POST['val']);
    $sql = "UPDATE " . $ecs->table('activity') . " SET is_sale = $is_sale WHERE parent_id = '$parent_id'";
    $db->query($sql);
    make_json_result($is_sale);
}
/* 设置回收状态 */
elseif($_REQUEST['act'] == 'toggle_on_delete'){
    admin_priv('gift_manage');
    $parent_id       = intval($_POST['id']);
    $is_delete        = intval($_POST['val']);
    if($is_delete == 1){/* 回收礼包 */
        // 加锁
        require_once(ROOT_PATH . 'includes/classLock.php');
        $lock = new classLock;
        $lockName = 'lockGift' . $parent_id;
        if ($lock->lock($lockName)) {
            delete_activity($parent_id);
            admin_log($post['name']."parent_id:$parent_id", 'remove', 'gift');
            // 释放锁
            $lock->unlock($lockName);
        }
    }else{
        $sql = "UPDATE " . $ecs->table('activity') . " SET is_delete = $is_delete WHERE parent_id = '$parent_id'";
        $db->query($sql);
    }
    make_json_result($is_delete);
}

/* 回收礼包 */
function delete_activity($parent_id = 0){
    if($parent_id > 0){
        /* 初始化库存类 */
        require_once(ROOT_PATH . 'includes/classStock.php');
        $cStock = new classStock();

        /* 获取所有购物车数量信息 */
        $sql = "SELECT sum(goods_number) FROM " . $GLOBALS['ecs']->table('cart') . " WHERE parent_id = '$parent_id'";
        $cart_number = $GLOBALS['db']->getOne($sql);

        $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') . " WHERE parent_id = '$parent_id'";
        $GLOBALS['db']->query($sql);
        /* 获取所有商品信息 */
        $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('activity_goods') . " WHERE parent_id = '$parent_id'";
        $goodsList = $GLOBALS['db']->getAll($sql);
        /* 获取所有发放信息 */
        $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('activity_stock') . " WHERE parent_id = '$parent_id' AND area_id = 3";
        $stockList = $GLOBALS['db']->getAll($sql);

        foreach ($stockList as $key => $value) {
            if($value['valid_number'] > 0 || $cart_number > 0 ){
                // 获取需要回收的数量
                $stock = $value['valid_number'] + $cart_number;
                foreach ($goodsList as $k => $v) {
                    $sql = "SELECT goods_sku FROM " . $GLOBALS['ecs']->table('goods_stock') . " gs WHERE gs.goods_id = '$v[goods_id]' AND gs.goods_attr_id = '$v[goods_attr_id]' AND area_id = '$value[area_id]'";
                    $goods_sku = $GLOBALS['db']->getOne($sql);
                    $stocks = $stock * $v['goods_number'];
                    $cStock->changeStock($goods_sku, $stocks);
                }
                /* 更新礼包数量 */
                $sql = "UPDATE " . $GLOBALS['ecs']->table('activity_stock') . " SET valid_number = 0, cart_number = 0, total_number = total_number - $stock WHERE parent_id = '$parent_id' AND area_id = '$value[area_id]'";
                $GLOBALS['db']->query($sql);
            }
        }
        /* 更新礼包状态 */
        $sql = "UPDATE " . $GLOBALS['ecs']->table('activity') . " SET is_delete = 1, is_sale = 0 WHERE parent_id = '$parent_id'";
        $GLOBALS['db']->query($sql);
    }
}

?>